''' Mission 10 - Reaction Tester Extra Spicy solution - 4B Option 1 Use the buttons on the left to select the indicator: pixels, screen color or images. This solution also includes the functions for intro and countdown. ''' from codex import * import random import time my_images = [pics.HEART, pics.HAPPY, pics.TIARA, pics.HOUSE, pics.MUSIC, pics.PLANE] my_colors = [RED, BLUE, GREEN, YELLOW, CYAN, PURPLE, CYAN, MAGENTA] def wait_button(): global choice while True: if buttons.was_pressed(BTN_A): break if buttons.was_pressed(BTN_L): choice = "L" break elif buttons.was_pressed(BTN_R): choice = "R" break elif buttons.was_pressed(BTN_U): choice = "U" break # -- add intro function def intro(): display.print("Reaction Tester") display.print("choose an") display.print("indicator") display.print("When displayed") display.print("press BTN A") display.print() display.print("Press A to start") wait_button() display.clear() def countdown(): # clear screen and countdown display.clear() pixels.set([BLACK, BLACK, BLACK, BLACK]) display.print("3", scale=6) time.sleep(1) display.print("2", scale=6) time.sleep(1) display.print("1", scale=6) time.sleep(1) display.clear() # -- call intro intro() # play the game 10 times while True: display.print("Choose:") display.print("L = pixels") display.print("R = colors") display.print("U = images") wait_button() countdown() # get random delay time ms = random.randrange(1000, 5000) delay_time = ms / 1000 time.sleep(delay_time) # turn pixels GREEN buttons.was_pressed(BTN_A) if choice == "L": pixels.set([GREEN, GREEN, GREEN, GREEN]) elif choice == "R": color = random.choice(my_colors) display.fill(color) elif choice == "U": image = random.choice(my_images) display.show(image) else: display.print("Press A again") # get start and end time start_time = time.ticks_ms() wait_button() end_time = time.ticks_ms() reaction_time = time.ticks_diff(end_time, start_time) display.print("Reaction Time: ") display.print(reaction_time)